;
(function($,win)
{
	/* @------------------------------------------------------------------------------------@ */
	$(document).ready(function()
	{
		var url = location.search;
		if (url.indexOf("?colorUuid=") != -1) {
			var reqPath = basePath+'color/queryColorById'+url;
			var sendBody = '{}';
			send(reqPath, "POST", "application/json", "json", sendBody, function(res)
			{
				if (res.result == 'success')
				{
					if (res.data != null)
					{
						var color = res.data;
						$("#color_uuid").val(color.colorUuid);
						$("#color_name").val(color.colorName);
						$("#description").val(color.description);
					} else
					{
						alert("没有查找到相关数据");
					}
				}
			});
		}else
		{
			//初始化列表
			initColorData(1,15);
		}
		$('#addnew').click(function()
		{

			window.location.href = jumpPath + 'view/udwin/color/addColor.jsp';
		});

		// 查询
		$('#btn_search').bind('click', function()
		{
			initColorData(1,15);
		});

		$('#tbn_save').bind('click', function()
		{
			saveColor();
		});
		
		$('#tbn_update').bind('click', function()
		{
			updateLoginUser();
		});

	});// the end

	/* @-----------------------------------------------------------------------------------------@ */
})(jQuery, window);

function editColor(colorUuid)
{
	window.location.href = jumpPath + 'view/udwin/color/editColor.jsp?colorUuid='+colorUuid;
}

function del(id)
{
	if (confirm("确定要删除吗？"))
	{
		var reqPath = basePath+'color/deleteColor?colorUuid='+id;
		var sendBody = '{"colorUuid": "'+id+'"}';
		send(reqPath, "POST", "application/json", "json", "{}", function(res)
		{
			if (res.result == 'success')
			{
				initColorData(1,15);
			}
		});
	}
}

function initColorData(pageNumber,pagesize)
{
	var reqPath = basePath+'color/queryPage';
	var sendBody = '{"curPage": 1,"pageSize": 15';
	var $tab = $('#colorTables tbody').html("");

	
	var reqPath = basePath + 'color/queryPage';
	var sendBody = '{"curPage": '+pageNumber+',"pageSize": '+pagesize+'';
	var $tab = $('#colorTables tbody').html("");
	
	var requestData = $('#frm_color').serializeObject();
	requestData = JSON.stringify(requestData);
	sendBody = sendBody+requestData.replace('{',',');
	
	send(reqPath, "POST", "application/json", "json", sendBody, function(res)
	{

		if (res.result == 'success')
		{

			if (res.data.data.length > 0)
			{
				var tpl_tr = $('#tpl_list').html(), rows = '';

				$.each(res.data.data, function(i,n)
				{
					var operation="<button onclick='editColor(\""+n.colorUuid+"\")' type='button' class='btn btn-primary'>编辑</button>"+
								  "<button onclick='del(\""+n.colorUuid+"\")' type='button' class='btn btn-primary'>删除</button>";
					rows += tpl_tr.replace(/\$colorUuid/g, n.colorUuid)
							      .replace(/\$colorName/,n.colorName)
							      .replace(/\$description/, n.description)
							      .replace(/\$operation/, operation);
				});
				$tab.append(rows);

				// page
				pagefunction(initColorData,res.data.totalPages,pagesize);

			} else
			{
				$tab.append('<tr><td colspan="7">没有查找到相关数据。</td></tr>');
			}
		}
	});

}

/**
 * 
 * @METHODNAME: saveLoginUser
 * @METDHOD:
 * @Description: 添加登陆用户
 * @author Administrator
 * @date 2015年1月10日 下午5:37:52
 */
function saveColor()
{
	
	var requsetPath = basePath + "color/updateColor";
	var requestData = $('#frm_color').serializeObject();
	requestData = JSON.stringify(requestData);
	send(requsetPath, "POST", "application/json", "json", requestData,
	function(resultData)
	{

		if (resultData.result == 'success')
		{
			// 跳转到列表
			window.location = jumpPath
					+ 'view/udwin/color/listColor.jsp';
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}


function updateLoginUser()
{
	
	var requsetPath = basePath + "userManager/updateUser.action";
	var requestData = $('#frm_login_user').serializeObject();
	requestData = JSON.stringify(requestData);
	// alert(requestData);
	send(requsetPath, "POST", "application/json", "json", requestData,
			function(resultData)
			{

				if (resultData.result == 'success')
				{
					// 跳转到列表
					window.location = jumpPath
							+ 'view/system/user/userlist.jsp';
				} else
				{
					alert(resultData.errorMsg);
				}
			});

}
